package demo5;
import org.junit.Test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component   //1.类似于Bean
             //2.Spring自动为此标识的类反射
/**
 * @Component/@Bean区别
 * 1.@C Spring 容器通过反射自动创建
 */
//@ComponentScan("demo5")//包扫描 扫描注解
public class User {

    //1.对象创建
    public User() {
        System.out.println("创建成功");
    }
    //2.方法初始化
    @PostConstruct
    public void init(){
        System.out.println("属性赋值");
    }
    //3.业务调用  手动
    public void hello(){
        System.out.println("业务手动调用");
    }
    //4.销毁方法
    @PreDestroy
    public void destroy(){
        System.out.println("销毁");
    }
    //单元测试方法

    @Test//
    public void testDemo6() {
            AnnotationConfigApplicationContext a = new AnnotationConfigApplicationContext(Spring1.class);
            User bean = a.getBean(User.class);
            bean.hello();
            //实现类中有关闭资源方法
            // 执行销毁方法，需要关闭资源
            a.close();
    }

    @Test
    public void tt(){
        AnnotationConfigApplicationContext aa = new AnnotationConfigApplicationContext(Spring2.class);
        User bean = aa.getBean(User.class);
        bean.hello();
        //关闭资源
        aa.close();
    }
    @Test//5.
    public void RR(){
        AnnotationConfigApplicationContext pp = new AnnotationConfigApplicationContext(PP.class);
        User bean = pp.getBean(User.class);
        bean.hello();
        //关闭资源
        pp.close();
    }
    @Test//6.@Component
    public void MM(){
        AnnotationConfigApplicationContext pp = new AnnotationConfigApplicationContext("demo5");
        User bean = pp.getBean(User.class);
        bean.hello();
        //关闭资源
        pp.close();
    }


}

//4.
@Configuration
@ComponentScan
class PP{
    @Bean
    public User user(){
        return new User();
    }
}








